<template>
  <div class="login-container">
    <div class="login-form">
      <div class="login-header">
        <h2>健身房后台管理系统</h2>
        <p>请登录您的账户</p>
      </div>

      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="form"
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            :prefix-icon="User"
            size="large"
            :maxlength="50"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            size="large"
            show-password
            :maxlength="50"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            size="large"
            :loading="loading"
            @click="handleLogin"
            class="login-button"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/modules/user'
import { preventDuplicateSubmit } from '@/utils'

const router = useRouter()
const userStore = useUserStore()

const loginFormRef = ref()
const loading = ref(false)

const loginForm = reactive({
  username: 'superadmin',
  password: 'Admin@123456'
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return

  await loginFormRef.value.validate(async (valid: boolean) => {
    if (valid) {
      try {
        await preventDuplicateSubmit('login', async () => {
          loading.value = true
          try {
            const success = await userStore.login({
              username: loginForm.username,
              password: loginForm.password
            })

            if (success) {
              ElMessage.success('登录成功')
              router.push('/dashboard')
            } else {
              ElMessage.error('登录失败，请检查用户名和密码')
            }
          } catch (error) {
            ElMessage.error('登录失败，请稍后重试')
            throw error // 重新抛出错误以确保防重复提交正确处理
          } finally {
            loading.value = false
          }
        })
      } catch (error: any) {
        // 防重复提交保护捕获的错误
        if (error.message === '请勿重复提交') {
          ElMessage.warning('请勿重复提交')
        }
      }
    }
  })
}
</script>

<style scoped lang="scss">
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100vh;
  min-height: 600px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  overflow: hidden;

  .login-form {
    width: 450px;
    min-width: 400px;
    padding: 50px 40px;
    background: #fff;
    border-radius: 10px;
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
    animation: slideUp 0.5s ease-out;

    .login-header {
      text-align: center;
      margin-bottom: 40px;

      h2 {
        font-size: 28px;
        font-weight: 600;
        color: #333;
        margin-bottom: 10px;
      }

      p {
        font-size: 15px;
        color: #666;
      }
    }

    .form {
      :deep(.el-form-item) {
        margin-bottom: 24px;
      }

      :deep(.el-input__wrapper) {
        padding: 8px 15px;
      }

      .login-button {
        width: 100%;
        height: 44px;
        font-size: 16px;
        font-weight: 500;
        margin-top: 10px;
      }
    }
  }
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式设计 - 虽然主要是PC端，但也要保证小屏幕可用 */
@media screen and (max-width: 768px) {
  .login-container {
    .login-form {
      width: 90%;
      min-width: 320px;
      padding: 40px 30px;
    }
  }
}
</style>
